package com.strongwell.awe.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.strongwell.awe.model.CmsCategoryEntity;
import com.strongwell.awe.service.CmsCategoryService;
import com.strongwell.awe.util.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import com.strongwell.awe.constant.Constants;
import com.strongwell.awe.util.Query;
import com.strongwell.awe.util.ResultUtil;
import com.strongwell.awe.base.controller.BaseController;
import com.strongwell.awe.annotation.SysLog;
import com.strongwell.awe.model.CmsLinkEntity;
import com.strongwell.awe.service.CmsLinkService;

/**
 * 友情链接
 * 
 * @author bridgezn
 * @email 39440421@qq.com
 * @date 2018-09-12 22:15:36
 */
@Controller
@RequestMapping("cmslink")
public class CmsLinkController extends BaseController{
	@Autowired
	private CmsLinkService cmsLinkServiceImpl;

	@Autowired
	private CmsCategoryService cmsCategoryServiceImpl;
	/**
	 * 页面
	 */
	@RequestMapping("/page")
	public String page(@RequestParam Map<String, Object> params){
		return "page/cmslink/cmslink";
	}
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(@RequestParam Map<String, Object> params){
	    Query query = new Query(params);
		List<CmsLinkEntity> cmsLinkList = cmsLinkServiceImpl.queryList(query);
		int total = cmsLinkServiceImpl.queryTotal(query);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",total,cmsLinkList);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping(value="/info/{id}", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object info(@PathVariable("id") Long id){
		CmsLinkEntity cmsLink = cmsLinkServiceImpl.queryObject(id);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",0,cmsLink);
	}

    /**
     * 增加页面
     */
    @RequestMapping("addcmslink")
    public String cmslinkAdd(HttpServletRequest req){
		List<CmsCategoryEntity> cmsCategoryEntity = cmsCategoryServiceImpl.queryNameList();
		req.setAttribute("category",cmsCategoryEntity);
        return "page/cmslink/addcmslink";
    }

    @RequestMapping("/checkCmsLinkName/{name}")
    @ResponseBody
    public ResultUtil checkCmsLinkName(@PathVariable("name")String name) {
            CmsLinkEntity cmslinks = cmsLinkServiceImpl.queryName(name);
        if(cmslinks!=null){
            return new ResultUtil(500,"名称已存在！");
        }
        return new ResultUtil(0);
    }
	/**
	 * 保存
	 */
	@RequestMapping(value="/save", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object save(HttpServletRequest req,@ModelAttribute CmsLinkEntity cmsLink){
		String weightdate = req.getParameter("DateString");
		Date date = DateUtil.convertToDate(weightdate);
		cmsLink.setWeightDate(date);
		cmsLinkServiceImpl.save(cmsLink);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",0,cmsLink);
	}

    /**
     * 修改待信息显示
     */

    @RequestMapping(value="/editcmslink/{id}")
    //@RequiresPermissions("cmslink:cmslink:update")
    public String editcmssite(HttpServletRequest req, @PathVariable("id")Long id) {
    	CmsLinkEntity cmslinks = cmsLinkServiceImpl.queryObject(id);
		String DateString = DateUtil.convertToString(cmslinks.getWeightDate());
        req.setAttribute("cmslink",cmslinks);
		req.setAttribute("DateString",DateString);
        return "page/cmslink/editcmslink";
    }
	/**
	 * 修改
	 */
	@RequestMapping(value="/update", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object update(HttpServletRequest req, @ModelAttribute CmsLinkEntity cmsLink){
		String weightdate = req.getParameter("DateString");
		Date date = DateUtil.convertToDate(weightdate);
		cmsLink.setWeightDate(date);
		int result = cmsLinkServiceImpl.update(cmsLink);
		return putMsgToJsonString(result,"",0,"");
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object delete(@RequestParam Long[] ids){
		int result = cmsLinkServiceImpl.deleteBatch(ids);
		return putMsgToJsonString(result,"",0,"");
	}

    @SysLog(value="删除文章信息")
    @RequestMapping("/delById/{id}")
    //@RequiresPermissions("cmslink:cmslink:delete")
    @ResponseBody
    public ResultUtil delById(@PathVariable("id")Long id) {

        try {
                cmsLinkServiceImpl.delById(id);
            return ResultUtil.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 批量删除指定管理员
     * @param //id
     * @return
     */
    @SysLog(value="批量删除文章信息")
    @RequestMapping("/delPatch/{Str}")
    //@RequiresPermissions("cmslink:cmslink:delete")
    @ResponseBody
    public ResultUtil delPatch(@PathVariable("Str")String cmslinkStr) {

        try {
                cmsLinkServiceImpl.delPatch(cmslinkStr);
            return ResultUtil.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }

    }
}
